﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Respawner : MonoBehaviour
{
    public GameObject circlePrefeb;

    [Header("DistanceNext")]
    public float minimumDistanceNext = 5f;
    public float maximumDistanceNext = 10f;
    [Header("DistanceColorChanger")]
    public float mininumDistanceChanger = 2.2f;
    public float maximumDistanceChanger = 4.8f;
    [Header("Scale")]
    public float minimumScale = 1f;
    public float maximumScale = 5f;
    [Header("Speed")]
    public float minimumRotateSpeed = 40f;
    public float maximumRotateSpeed = 200f;

    Vector3 v;

    private void Start()
    {
        Respawn();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "ColorChanger")
        {
            Respawn();
        }
    }

    void Respawn()
    {
        //RandomScale
        float randomScale = Random.Range(minimumScale, maximumScale);
        //RandomDistancePosition
        v = new Vector3(transform.position.x, transform.position.y + 1f + Random.Range(minimumDistanceNext, maximumDistanceNext), transform.position.z);
        GameObject circle = Instantiate(circlePrefeb, v, Quaternion.identity) as GameObject;
        circle.transform.localScale = new Vector3(randomScale, randomScale, 0);
        //RandomDistanceChanger
        circle.transform.GetChild(0).localPosition = new Vector3(0, Random.Range(mininumDistanceChanger, maximumDistanceChanger), 0);
        //RandomRotateSpeed
        if (Random.value >= 0.5)
        {
            circle.transform.GetChild(1).GetComponent<Rotater>().speed = Random.Range(minimumRotateSpeed, maximumRotateSpeed);
        }
        else
        {
            circle.transform.GetChild(1).GetComponent<Rotater>().speed = Random.Range(-minimumRotateSpeed, -maximumRotateSpeed);
        }
    }
}
